const base64 = "";

export default class Ellipse {
  constructor(basicCesium) {
    this.basicCesium = basicCesium
    this.viewer = basicCesium.viewer;

    this.entities = this.viewer.entities;
    this.primitives = this.viewer.scene.primitives;
    this.entity = null;

    this.initTime();
    this.initEntity();
  }

  initTime() {
    this.start = Cesium.JulianDate.fromDate(new Date());
    this.viewer.clock.startTime = this.start.clone();
    this.viewer.clock.currentTime = this.start.clone();
    this.viewer.clock.clockRange = Cesium.ClockRange.CLAMPED; //Loop at the end
    this.viewer.clock.multiplier = 10;
  }
  initEntity() {
    let start = [
      [118.802891, 31.9773, 0],
      [118.77242, 31.913078, 0],
      [118.783344, 31.899346, 0],
    ];
    //起点
    this.entities.add({
      position: this.basicCesium.lnglatToWorld(start[0][0], start[0][1]),
      billboard: {
        image: base64
      },
    })
    //飞机
    this.entity = this.entities.add({
      position: this.basicCesium.lnglatToWorld(start[1][0], start[1][1]),
      model: {
        uri: "glb/balloon.glb",
        minimumPixelSize: 100,
        maximumScale: 20000,
      },
      scale: 100,
      disableDepthTestDistance: 99000000,
    })

    //控制线
    let isConstant = false;
    this.polyline = this.entities.add({
      polyline: {
        positions: new Cesium.CallbackProperty((time, _) => {
          let s = this.basicCesium.lnglatToWorld(start[0][0], start[0][1]);
          let e = this.entity.position.getValue(time);
          if (e == undefined) {
            e = this.basicCesium.lnglatToWorld(start[1][0], start[1][1]);
          }
          return [e, s]
        }, isConstant),
        width: 2,
        material: new Cesium.PolylineDashMaterialProperty({
          color: Cesium.Color.DARKRED,
          dashLength: 10
        })
      }
    })
    this.beginAnimate()
  }

  beginAnimate(isTrue = true) {
    this.viewer.clock.shouldAnimate = isTrue;
    if (isTrue && this.entity != null && this.entity.position instanceof Cesium.ConstantPositionProperty) {
      let positions = this.computedProperty();
      this.entity.availability = new Cesium.TimeIntervalCollection([new Cesium.TimeInterval({
        start: this.start,
        stop: this.stop
      })]);
      this.entity.position = positions;
      this.entity.orientation = new Cesium.VelocityOrientationProperty(positions);
      this.entity.path = {
        resolution: 1,
        material: new Cesium.PolylineGlowMaterialProperty({
          glowPower: 0.2,
          color: Cesium.Color.YELLOW
        }),
        width: 5
      };
      this.entity.ellipse = {
        semiMinorAxis: 12000.0,
        semiMajorAxis: 12000.0,
        material: Cesium.Color.fromCssColorString('#A64968').withAlpha(0.4),
        show: new Cesium.CallbackProperty((time, res) => {
          let pos = this.entity.position.getValue(time);
          let longlat = this.basicCesium.worldToLnglat(pos);
          let height = longlat.height;
          if (this.stop && time.secondsOfDay == this.stop.secondsOfDay) {
            this.entity.position = this.basicCesium.lnglatToWorld(118.802891, 31.9773);
            this.entity.path = null;
            this.entity.availability = null;
            this.entity.orientation = null;
          }
          return height >= 500;
        }, false)
      };
      this.entity.position.setInterpolationOptions({
        // 直线
        interpolationDegree: 1,
        interpolationAlgorithm: Cesium.LinearApproximation
      });

    }
  }

  computedProperty() {
    let property = new Cesium.SampledPositionProperty();
    let positions = [
      [118.77242, 31.913078, 0],
      [118.673535, 31.87261, 200],
      [118.60627, 31.818134, 800],
      [118.548203, 31.73513, 1200],
      [118.663187, 31.697778, 4000],
      [118.787368, 31.729725, 3000],
      [118.876768, 31.973134, 600],
      [118.77242, 31.913078, 0],
    ];
    let currentTime = this.start;
    for (let i = 0; i < positions.length; i++) {
      let position = this.basicCesium.lnglatToWorld(positions[i][0], positions[i][1], positions[i][2]);
      let time = Cesium.JulianDate.addSeconds(currentTime, Cesium.Math.nextRandomNumber() * 60 + 60, new Cesium.JulianDate());
      if (i == 0) {
        property.addSample(this.start, position);
      } else {
        property.addSample(time, position);
      }
      currentTime = time;
    }
    this.stop = currentTime;
    this.viewer.clock.stopTime = this.stop.clone();
    this.viewer.timeline.zoomTo(this.start, this.stop);
    return property;
  }
}